package com.td.framework.utils;

/**
 * 功能：数字 变成 汉字
 *       
 * -----------------------------------
 * 说明： 
 *        最高千兆
 *        小数点后两位有效        
 * -----------------------------------
 * 如：
 *    123          变成 零佰贰拾叁
 *    零佰贰拾叁   变成 123
 * -----------------------------------
 * 2003.12.09下载别人程序,并修改
 */

public class NumberToChinese {

  public static String ChangeToBig(String CurrentNumber){
       //如果为负数
       int flag =0;
       if(CurrentNumber.indexOf("-")!=-1) {
         CurrentNumber = CurrentNumber.substring(1,CurrentNumber.length());
         ++flag;	
       }
       //若有"+"号
       if(CurrentNumber.indexOf("+")!=-1) {
         CurrentNumber = CurrentNumber.substring(1,CurrentNumber.length());	
       }       
       
       	
      //输入CurrentNumber的数字格式为%*.**,小数点后第三位起忽略不记;
      if(CurrentNumber.indexOf(".")<0)
        CurrentNumber = CurrentNumber + ".00";
      else if((CurrentNumber.length()-CurrentNumber.indexOf("."))==2)
        CurrentNumber = CurrentNumber + "0";
      else if((CurrentNumber.length()-CurrentNumber.indexOf("."))>2){
        String strTemp = CurrentNumber.substring(CurrentNumber.indexOf("."),CurrentNumber.length());
        CurrentNumber = CurrentNumber.substring(0,CurrentNumber.indexOf("."))+strTemp.substring(0,3);
        //System.out.println(CurrentNumber);
      }

      String results=new String("￥");
      
      //若为负数
      if(flag==1) 
        results =  results+"负";  
          
      String temp = CurrentNumber;
      final String Units[]={"分","角","圆","拾","佰","仟",   //单位
                            "万","拾","佰","仟","亿","拾","佰","仟","兆","拾","佰","仟"};

      final String Numbers[]={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"}; //大写数
      java.lang.StringBuffer buf=new java.lang.StringBuffer(); //处理后的字符串
      char c;
      
      //去掉小数点
      for(int i=0;i<temp.length();i++){  
        c=temp.charAt(i);
        if(c!='.')
          buf.append(c);
      }
      
      //进行转换
      for(int i=1;i<=buf.length();i++){
        results+=Numbers[buf.charAt(i-1)-48];
        results+=Units[buf.length()-i];
      }
      
      //消除不合习惯的汉字
      results = results.replaceAll("零拾零","零");
      results = results.replaceAll("零佰零","零");
      results = results.replaceAll("零仟零","零");
      results = results.replaceAll("零拾","零");
      results = results.replaceAll("零佰","零");
      results = results.replaceAll("零仟","零");
      results = results.replaceAll("零万","万");
      results = results.replaceAll("零亿","亿");
      results = results.replaceAll("亿万","亿");
      results = results.replaceAll("点","圆");
      results = results.replaceAll("角分","角");
      results = results.replaceAll("零分","");
      results = results.replaceAll("零角","");

      if(Long.parseLong(CurrentNumber.substring(0,CurrentNumber.indexOf(".")))==0) {
        if(Long.parseLong(CurrentNumber.substring(CurrentNumber.indexOf(".")+1,CurrentNumber.length()))!=0) {
          results = results.replaceAll("零圆","");
        }
      }
      else {
        results = results.replaceAll("零圆","圆");
      }
      
      //如果小数点后无有效数字,则加"整"字输出
      if(Long.parseLong(CurrentNumber.substring(CurrentNumber.indexOf(".")+2,CurrentNumber.length()))==0)
        results = results + "整";
        
      
      return results;
  }
  
  //测试,输出
  public static void main(String[] args) throws Exception {
    NumberToChinese t = new NumberToChinese();
    try {
      String str = "10008.765";
      System.out.println(str);
      System.out.println(t.ChangeToBig(str));
   System.err.println(2000100010.33);
   System.err.println(t.ChangeToBig("9999999999999999.33"));      
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println(e.getMessage());
    }
  }  
}